<?php
/**
 * Copyright 2008 francissem (http://www.webmoss.net)(francissem@gmail.com)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @author francissem
 * @copyright 2008 francissem (http://www.webmoss.net)(francissem@gmail.com)
 * @license http://www.apache.org/licenses/LICENSE-2.0
 * @example
 *  $system_setting = Core_Setting::factory("system",$setting_arr);
 *  $system_setting["test"]=1;
 * 	$system_setting->save();
 */
require_once(dirname(__FILE__)."/Repository/Agent/Interface.php");

class Core_Setting extends ArrayIterator implements Core_Repository_Agent_Interface {

	private $name;
	private $default_setting_arr;

	/**
	 * implement repository functions
	 */
	private static $repos_pool;
	static function addRepository($name,Core_Repository_Abstract $repos){
		self::$repos_pool[$name]=$repos;
	}
	static function getRepository($name){
		return self::$repos_pool[$name];
	}
	static function getRepositories(){
		return self::$repos_pool;
	}

	/**
	 * Create new setting object
	 * @example Setting::factory("File",array("path"=>"/somewhere.ini"))
	 *
	 */

	static $list;
	static function register($name,Core_Setting $setting){
		if(!isset(self::$list[$name])){
			self::$list[$name] = $setting;
		}else{
			throw new Core_Setting_Exception("Setting $name Already Exists, use getFromRegister to acquire the setting");
		}
	}
	static function getFromRegister($name){
		return self::$list[$name];
	}

	function __construct($name,$setting_arr){
		$this->name = $name;
		$this->default_setting_arr = $setting_arr;
		$repos = self::$repos_pool["override"];
		$repos->selectCursor($this->name);
		$override_setting=array();
		//$override_setting = $repos->acquireDataObject()->getContent();
		//if(!isset($override_setting)) $override_setting=array();
		parent::__construct(array_merge($setting_arr,$override_setting));
		self::register($name,$this);
	}


	function save($tags=NULL){
		$repos = self::$repos_pool["override"];
		$repos->selectCursor($this->name);
		$repos->saveDataObject(new Core_Repository_Data_New_Array((array)$this,true),$tags);
	}

}
?>